package com.gryffonx.NameGenerators;

import com.gryffonx.DiceRoller.StaticDiceRoller;
import com.gryffonx.utils.StringUtils;
import com.gryffonx.utils.XMLUtils;
import com.gryffonx.Interfaces.generators.NameGeneratorsBinding;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.SAXReader;

import java.io.File;

/**
 * Currently has 14 different ways to generate a city name.  A random number is generated to determine
 * the rule to be used.  Once the rule has been identified, the name is built using further methods that
 * depend on random numbers.  The rules are simply a series of arrangements of prefix, mid, and suffix elements
 * with different grammatical treatments
 *
 * @author Erik Stell
 * @version 1.0
 */
public class OrganizationNameGenerator {
    private static final File fileToBeParsed = new File("src/com/gryffonx/xml/organizations.xml");
    private static final SAXReader reader = new SAXReader();
    private static Document xmlDoc = null;
    private static NameGeneratorsBinding parentWindow = null;

    public static String generateName(int howMany, NameGeneratorsBinding parent) {
        parentWindow = parent;
        StringBuffer retVal = new StringBuffer();

        if (howMany <= 0) {
            howMany = 1;
        }

        try {
            xmlDoc = reader.read(fileToBeParsed);
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

        for (int i = 0; i < howMany; i++) {
            StringBuffer finishedName = new StringBuffer();
            try {
                int rule = determineRule();
                if (rule == 1) {
                    finishedName.append(generateRule1());
                } else if (rule == 2) {
                    finishedName.append(generateRule2());
                } else if (rule == 3) {
                    finishedName.append(generateRule3());
                } else if (rule == 4) {
                    finishedName.append(generateRule4());
                } else if (rule == 5) {
                    finishedName.append(generateRule1());
                } else if (rule == 6) {
                    finishedName.append(generateRule6());
                }

            } catch (Exception e) {
                e.printStackTrace();
            }

            retVal.append(StringUtils.capitalize(finishedName.toString()));
            // retVal.append("\n");
        }
        return retVal.toString();
    }

    /**
     * Generates a city name using a standard prefix, 2 middle elements, and a suffix.
     *
     * @return
     */
    private static String generateRule1() {
        StringBuffer retVal = new StringBuffer();

        retVal.append(getPrefix(1));
        retVal.append(" of the ");
        retVal.append(getSuffix(3));

        return retVal.toString();
    }

    /**
     * Generates a city name using a standard prefix and a suffix.
     *
     * @return
     */
    private static String generateRule2() {
        StringBuffer retVal = new StringBuffer();
        int group = StaticDiceRoller.rolldSomething(3, 1);
        retVal.append(getPrefix(1));
        retVal.append(" of the ");
        retVal.append(getSuffix(group));
        retVal.append(" ");        
        retVal.append(getSuffix(3));

        return retVal.toString();
    }

    /**
     * Generates a city name using a standard prefix, middle element, and a suffix.
     *
     * @return
     */
    private static String generateRule3() {
        StringBuffer retVal = new StringBuffer();
        int group = StaticDiceRoller.rolldSomething(3, 1);        
        retVal.append(getSuffix(group));
        retVal.append(" ");       
        retVal.append(getPrefix(1));
        retVal.append(" of the ");
        retVal.append(getSuffix(3));

        return retVal.toString();
    }

    /**
     * Generates a city name using a standard prefix, middle element from series 2, and a suffix.
     *
     * @return
     */
    private static String generateRule4() {
        StringBuffer retVal = new StringBuffer();
        int group = StaticDiceRoller.rolldSomething(3, 1);
        retVal.append(getPrefix(1));
        retVal.append(" of the ");
        retVal.append(getSuffix(group));
        retVal.append(" ");
        retVal.append(getSuffix(3));

        return retVal.toString();
    }

    /**
     * Generates a city name using a standard prefix, middle element from series 2, and a suffix.
     *
     * @return
     */
    private static String generateRule5() {
        StringBuffer retVal = new StringBuffer();
        int group = StaticDiceRoller.rolldSomething(3, 1);
        retVal.append(getPrefix(1));
        retVal.append(" of the ");
        retVal.append(getAltPrefix(1));
        //retVal.append(" ");
       // retVal.append(getAltPrefix(3));
        return retVal.toString();
    }

    /**
     * Generates a city name using a standard prefix, middle element from series 2, and a suffix.
     *
     * @return
     */
    private static String generateRule6() {
        StringBuffer retVal = new StringBuffer();
        int group = StaticDiceRoller.rolldSomething(3, 1);
        retVal.append("The ");
        retVal.append(getSuffix(1));
        retVal.append(" ");
        retVal.append(getPrefix(1));

        return retVal.toString();
    }

    private static int determineRule() throws Exception {
        int retVal = StaticDiceRoller.rolldSomething(6, 1, true);
        return retVal;
    }

    private static String getPrefix(int group) {
        String retVal = "";
        try {
            retVal = StringUtils.capitalize(XMLUtils.getNodeValue("prefix", xmlDoc, group, parentWindow));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return retVal;
    }
    private static String getAltPrefix(int group) {
        String retVal = "";
        try {          
            retVal = StringUtils.capitalize(TitleGenerator.generateName(1));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return retVal;
    }
    private static String getSuffix(int group) {
        String retVal = "";
        try {
            retVal = StringUtils.capitalize(XMLUtils.getNodeValue("suffix", xmlDoc, group, parentWindow));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return retVal;
    }
}